package edu.scau.web.sku;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.sku.service.SkuService;
import edu.scau.sku.vo.SkuVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存Controller
 */
@Tag(name = "库存管理", description = "库存管理接口")
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Operation(
        summary = "查找所有库存",
        description = "查找所有未删除的库存,访问权限:ALL",
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuVo.class))),
            @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<SkuVo> skuList = skuService.listAll();
        return AjaxResult.success(skuList);
    }

    @Operation(
        summary = "分页查找",
        description = "分页查找所有库存,访问权限:ALL",
        parameters = {
            @Parameter(name = "pageNum", description = "页码", required = true, schema = @Schema(type = "int")),
            @Parameter(name = "pageSize", description = "每页数据条数", required = true, schema = @Schema(type = "int")),
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuVo.class))), // 返回 PageInfo 对象
            @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))) // 返回 AjaxResult 对象
        }
    )
    @GetMapping("/listByPage")
    public AjaxResult listByPage(
        @RequestParam("pageNum") Integer pageNum,
        @RequestParam("pageSize") Integer pageSize
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<SkuVo> skuList = skuService.listAll();
        PageInfo<SkuVo> pageInfo = new PageInfo<>(skuList);
        return AjaxResult.success(pageInfo);
    }

    @Operation(
            summary = "通过条件查询查找",
            description = "分页查找所有库存,访问权限:ALL",
            requestBody = @RequestBody(
                    description = "查询条件,code是必需的",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = SkuVo.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuVo.class))), // 返回 PageInfo 对象
                    @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))) // 返回 AjaxResult 对象
            }
    )
    @PostMapping("/listAllByParams")
    public AjaxResult listAllByParams(@org.springframework.web.bind.annotation.RequestBody SkuVo skuVo
    ) {
        List<SkuVo> skuList = skuService.listByPage(skuVo);
        return AjaxResult.success(skuList);
    }

    @Operation(
        summary = "根据code查找库存",
        description = "根据code查找信息,访问权限:ALL",
        parameters = {
            @Parameter(name = "skuCode", description = "库存编号skuCode", required = true, schema = @Schema(type = "String"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuVo.class))),
            @ApiResponse(responseCode = "204", description = "不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @GetMapping("/findSkuById")
    public AjaxResult findSkuById(@RequestParam("skuCode") String skuCode) {
        SkuVo sku = skuService.findSkuByCode(skuCode);
        if (sku != null) {
            return AjaxResult.success(sku);
        } else {
            return AjaxResult.success("资源不存在或已删除！", HttpStatus.NO_CONTENT);
        }
    }

    @Operation(
        summary = "更新库存信息",
        description = "根据skuCode更新信息,SkuCode字段必须,访问权限:库存、admin",
        requestBody = @RequestBody(
            description = "更新条件,code是必需的",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = SkuVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "更新失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('IM', 'admin')")
    public AjaxResult updateSku(@org.springframework.web.bind.annotation.RequestBody SkuVo sku) {
        try {
            int res = skuService.updateSku(sku);
            return res > 0 ? AjaxResult.success(HttpStatus.SUCCESS) : AjaxResult.error("更新失败");
        }
        catch (IllegalArgumentException e){
            return AjaxResult.error(HttpStatus.BAD_REQUEST,e.getMessage());
        }
    }

    @Operation(
        summary = "插入库存",
        description = "插入库存信息,code必需,访问权限:库存、admin",
        requestBody = @RequestBody(
            description = "插入条件",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = SkuVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "插入失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @Hidden
    @PutMapping("/insert")
    @PreAuthorize("hasAnyAuthority('IM', 'admin')")
    public AjaxResult insertSku(@org.springframework.web.bind.annotation.RequestBody SkuVo sku) {
        try {
            int res = skuService.insertSku(sku);
            return res > 0 ? AjaxResult.success("插入成功！") : AjaxResult.error("插入失败！");
        }
        catch (IllegalArgumentException e){
            return AjaxResult.error(HttpStatus.BAD_REQUEST,e.getMessage());
        }
    }

    @Operation(
        summary = "根据Code删除",
        description = "根据Code删除信息,访问权限:库存、admin",
        parameters = {
            @Parameter(name = "skuCode", description = "库存编号skuCode", required = true, schema = @Schema(type = "String"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @DeleteMapping("/delete")
    @PreAuthorize("hasAnyAuthority('IM', 'admin')")
    public AjaxResult delete(@RequestParam("skuCode") String skuCode) {
        int res = skuService.deleteSkuByCode(skuCode);
        return res > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    @Operation(
            summary = "查找空库存数据",
            description = "查找所有库存为0的数据,访问权限:库存、admin",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuVo.class))),
                    @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @GetMapping("/findEmpty")
    @PreAuthorize("hasAnyAuthority('admin','IM')")
    public AjaxResult findAllStockEmpty(){
        return AjaxResult.success(skuService.findAllStockEmpty());
    }
}
